package tech.aistar.json;

import com.alibaba.fastjson.JSONArray;
import tech.aistar.prj.entity.Pet;
import tech.aistar.prj.entity.Result;
import tech.aistar.prj.service.IPetService;
import tech.aistar.prj.service.impl.PetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.util.List;

/**
 * 本类功能:
 *
 * @author cxylk
 * @date 2020/9/26 15:38
 */
@WebServlet(urlPatterns = "/json/pets")
public class PetsAction extends HttpServlet {
    private IPetService petService=new PetServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        PrintWriter out=resp.getWriter();
        String name=req.getParameter("name");
        List<Pet> pets=petService.search(null,name);
        Result result=new Result();
        if(null!=pets&&pets.size()>0){
            result.setCode("200");
            result.setMsg("查询成功");
            result.setData(pets);
        }else{
            result.setCode("500");
            result.setMsg("查询失败");
        }
        String jsonStr= JSONArray.toJSONString(result);
        out.write(jsonStr);
    }
}
